package com.sanjiahantang.ythshopping.activity.me;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.model.BaseModel;
import com.sanjiahantang.ythshopping.model.LeaveMsgModle;
import com.sanjiahantang.ythshopping.presenter.MyLeavelistPresenter;
import com.sanjiahantang.ythshopping.presenter.SaveMsgPresenter;
import com.sanjiahantang.ythshopping.views.LeaveMsgView;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.shopping.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.shopping.uilibrary.views.LoadMoreScrollView;
import com.sanjiahantang.shopping.uilibrary.views.MyListView;

public class LeaveMsgActivity extends Activity implements View.OnClickListener,LeaveMsgView{
    TextView tv_title;
    EditText et_leavetitle,et_main;
    ImageView btn_back,iv_leavet,iv_complaint,iv_askt,iv_aftert,iv_findt;
    LinearLayout ll_mylist,ll_leavet,ll_complaint,ll_askt,ll_aftert,ll_findt;
    MyListView lv_mylist;
    Button btn_submit;
    LoadMoreScrollView sv_leave;




    MyLeavelistPresenter myleavelistPresenter;

    QuickAdapter<LeaveMsgModle.LeaveD> adapter;

    int leavetype=0,page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave_msg);


    }

    public void InitView()
    {
        tv_title= (TextView) findViewById(R.id.title);
        btn_back= (ImageView) findViewById(R.id.back);
        ll_mylist= (LinearLayout) findViewById(R.id.ll_leave_mylist);
        lv_mylist= (MyListView) findViewById(R.id.lv_leave_mylist);
        ll_aftert= (LinearLayout) findViewById(R.id.ll_leave_typeafter);
        ll_askt= (LinearLayout) findViewById(R.id.ll_leave_typeask);
        ll_complaint= (LinearLayout) findViewById(R.id.ll_leave_typecomplaint);
        ll_findt= (LinearLayout) findViewById(R.id.ll_leave_typefind);
        ll_leavet= (LinearLayout) findViewById(R.id.ll_leave_typeleave);
        iv_aftert= (ImageView) findViewById(R.id.iv_leave_typeafter);
        iv_askt= (ImageView) findViewById(R.id.iv_leave_typeask);
        iv_complaint= (ImageView) findViewById(R.id.iv_leave_typecomplaint);
        iv_findt= (ImageView) findViewById(R.id.iv_leave_typefind);
        iv_leavet= (ImageView) findViewById(R.id.iv_leave_typeleave);
        et_leavetitle= (EditText) findViewById(R.id.et_leave_title);
        et_main= (EditText) findViewById(R.id.et_leave_content);
        btn_submit= (Button) findViewById(R.id.btn_leave_submit);
        sv_leave= (LoadMoreScrollView) findViewById(R.id.sv_leave);
        sv_leave.loadding=true;

        tv_title.setText("我的留言");
        btn_back.setOnClickListener(this);
        ll_complaint.setOnClickListener(this);
        ll_leavet.setOnClickListener(this);
        ll_findt.setOnClickListener(this);
        ll_askt.setOnClickListener(this);
        ll_aftert.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        sv_leave.setOnBorderListener(new LoadMoreScrollView.OnBorderListener()
        {

            @Override
            public void onBottom() {
                sv_leave.loadding=true;
                page++;
                myleavelistPresenter.LoadLeaveMsg(page);
            }

            @Override
            public void onTop() {

            }
        });



        myleavelistPresenter=new MyLeavelistPresenter(this,this);
        myleavelistPresenter.LoadLeaveMsg(1);
        adapter=new QuickAdapter<LeaveMsgModle.LeaveD>(this,R.layout.myleave_item) {
            @Override
            protected void convert(BaseAdapterHelper helper, LeaveMsgModle.LeaveD item) {
                helper.setText(R.id.tv_leaveitem_main,item.getMsg_content());
                helper.setText(R.id.tv_leaveitem_time,item.getMsg_time());
                helper.setText(R.id.tv_leaveitem_title,"标题："+item.getMsg_title());
                helper.setText(R.id.tv_leaveitem_type,item.getMsg_type());

            }
        };

    }

    @Override
    protected void onResume() {
        super.onResume();
        page=1;
        InitView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.ll_leave_typeleave:
                setLeaveType(true, false, false, false, false);
                leavetype = 0;
                break;
            case R.id.ll_leave_typecomplaint:
                setLeaveType(false, true, false, false, false);
                leavetype = 1;
                break;
            case R.id.ll_leave_typeask:
                setLeaveType(false, false, true, false, false);
                leavetype = 2;
                break;
            case R.id.ll_leave_typeafter:
                setLeaveType(false, false, false, true, false);
                leavetype = 3;
                break;
            case R.id.ll_leave_typefind:
                setLeaveType(false, false, false, false, true);
                leavetype = 4;
                break;
            case R.id.btn_leave_submit:

                SaveMsgPresenter presenter = new SaveMsgPresenter(this, this);
                if (et_main.getText().toString().equals("") || et_leavetitle.getText().toString().equals(""))
                {
                    Toast.makeText(this,"填写内容不能为空",Toast.LENGTH_LONG).show();

                }else {
                    presenter.LoadsaveStatic(leavetype,et_leavetitle.getText().toString(),et_main.getText().toString());
                }

                break;

        }
    }

    @Override
    public void getLeavelist(LeaveMsgModle model) {

        if (model.getCode().equals("1") )
        {
            ll_mylist.setVisibility(View.VISIBLE);
            lv_mylist.setAdapter(adapter);
            if (page==1)
            {
                adapter.clear();
            }
            adapter.addAll(model.getData().getMsg());

            sv_leave.loadding=false;

        }else {
            sv_leave.loadding=true;
            if (page==1)
            {
                ll_mylist.setVisibility(View.GONE);
            }

        }

    }

    @Override
    public void returnLeavestatic(BaseModel model) {

        if (model.getCode().equals("1"))
        {
            Toast.makeText(this,model.getMsg(),Toast.LENGTH_LONG).show();
            onResume();
        }
    }

    public void setLeaveType(boolean leave,boolean complain,boolean ask,boolean after,boolean find)
    {
        if (leave)
        {
            iv_leavet.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
        }else
        {
            iv_leavet.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
        }
        if (complain)
        {
            iv_complaint.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
        }else
        {
            iv_complaint.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
        }
        if (ask)
        {
            iv_askt.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
        }else
        {
            iv_askt.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
        }
        if (after)
        {
            iv_aftert.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
        }else
        {
            iv_aftert.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
        }
        if (find)
        {
            iv_findt.setImageDrawable(getResources().getDrawable(R.drawable.select_yes));
        }else
        {
            iv_findt.setImageDrawable(getResources().getDrawable(R.drawable.select_no));
        }

    }


}
